package com.renwen.sms.controller;

import com.renwen.sms.util.VerifyCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码控制器
 */
@RestController
public class VerifyCodeController {
    @GetMapping("/code")
    public void code(HttpServletResponse response) {
        String codeString = VerifyCodeUtil.generateVerifyCode(4);

        BufferedImage buffImg = VerifyCodeUtil.outputImage(null, null, codeString);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/png");
        try {
            ImageIO.write(buffImg, "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
